package com.teame.readbaby.net;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.GloableParams;
import com.teame.readbaby.vo.Userinfo;



public class HttpClientAdapter {
	private HttpClient client;
	private HttpRequest request;
	private HttpGet get;
	private HttpPost post;
	private HttpResponse response;
	public static BasicHeader[] headers = new BasicHeader[10];
	static {
		headers[0] = new BasicHeader("Appkey", "12343");
		headers[1] = new BasicHeader("Udid", "");// 手机串号
		headers[2] = new BasicHeader("Os", "android");//
		headers[3] = new BasicHeader("Osversion", "");//
		headers[4] = new BasicHeader("Appversion", "");// 1.0
		headers[5] = new BasicHeader("Sourceid", "");//
		headers[6] = new BasicHeader("Ver", "");
		headers[7] = new BasicHeader("Userid", "");
		headers[8] = new BasicHeader("Usersession", "");
		headers[9] = new BasicHeader("Unique", "");
	}
	
	public HttpClientAdapter()
	{
		client=new DefaultHttpClient();
		//dpt:判断手机的上网方式
		if(StringUtils.isNotBlank(GloableParams.PROXY_IP))
		{
			//wap方式
			HttpHost host=new HttpHost(GloableParams.PROXY_IP, GloableParams.PROXY_PORT);
			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host);
		}
	}
	/**
	 * dpt
	 * @param url 地址
	 * @param params 参数
	 * @return json 字符串
	 */
	public String sendPost(String url,Map<String,String> params)
	{
		if(GloableParams.userInfo!=null){ //gyh:我在GloableParams里放了userInfo,如果用户登录了就不是null,每次post之前都检查一次,如果有用户登录了则在头里带上用户信息
			headers[7] = new BasicHeader("Userid",GloableParams.userInfo.getUserId() );
			headers[8] = new BasicHeader("Usersession", GloableParams.userInfo.getUsersessio());
		}
		
		post=new HttpPost(url);
		
		HttpParams httpParams = new BasicHttpParams();//
		//params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 8000); // 连接超时
		HttpConnectionParams.setSoTimeout(httpParams, 5000); // 响应超时
		post.setParams(httpParams);
		
		post.setHeaders(headers);
		
		try {
			//设置参数信息
			if(params!=null)
			{
				List<BasicNameValuePair> pairs=new ArrayList<BasicNameValuePair>();
				for(Map.Entry<String, String> item:params.entrySet())
				{
					BasicNameValuePair pair=new BasicNameValuePair(item.getKey(), item.getValue());
					pairs.add(pair);
				}
				
				HttpEntity entity=new UrlEncodedFormEntity(pairs, ConstantValue.CHARSET);
				post.setEntity(entity);
			}
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(response.getEntity(), ConstantValue.CHARSET);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	

}
